package com.fast.cron.db.service;

import com.fast.cron.db.RowMapper;
import com.fast.cron.db.dao.AbstractDao;
import com.fast.cron.db.dao.JobLogDao;
import com.fast.cron.db.entry.JobLog;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Leo Jie
 * @date: 2018-10-04 21:04
 * @version: v1.0
 */
public class JobLogService extends AbstractDao implements JobLogDao {
    @Override
    public long addJobLog(JobLog jobLog) throws SQLException {
        String sql = "insert fast_cron_job_log(execution_job_id,job_log)values (?,?);";
        return insert(sql, jobLog.getExecutionJobId(), jobLog.getJobLog());
    }

    @Override
    public JobLog messageOfLog(int executionJobId) throws SQLException {
        String sql = "select id,execution_job_id, job_log from fast_cron_job_log where execution_job_id = ? limit 1";
        return queryOne(JobLog.class, sql, new JobLogRowMapper().rowMapperMap(), executionJobId);
    }

    private class JobLogRowMapper implements RowMapper {
        @Override
        public Map rowMapperMap() {
            Map<String, String> rowMap = new HashMap<>(2);
            rowMap.put("execution_job_id", "executionJobId");
            rowMap.put("job_log", "jobLog");
            return rowMap;
        }
    }
}